import Vue from 'vue'
import VueRouter from 'vue-router'

// 用户端组件
import UserHomeView from '../views/user/HomeView.vue'
import UserLoginView from '@/views/user/LoginView.vue'
import UploadImageView from '@/views/user/UploadImageView.vue'
import HistoryView from '@/views/user/HistoryView.vue'

// 管理端组件
import AdminHomeView from '@/views/admin/HomeView.vue'
import AdminLoginView from '@/views/admin/LoginView.vue'
import OrderListView from '@/views/admin/OrderListView.vue'
import VerificationCode from '@/views/admin/VerificationCodeView.vue'
import AccountView from '@/views/admin/AccountView.vue'


import TestView from '@/views/TestView.vue'

Vue.use(VueRouter)

const routes = [

  {
    path:'/test',
    component: TestView

  },

  // 用户端主页面
  {
    path: '/home',
    name: 'UserHome',
    component: UserHomeView,
    children: [
      {
        path: 'userUpload',
        name: 'UserUpload',
        component: UploadImageView
      },
      {
        path: 'userHistory',
        name: 'UserHistory',
        component: HistoryView
      },
    ]
  },
  {
    path: '/login',
    name: 'UserLogin',
    component: UserLoginView
  },

  // 管理端
  {
    path: '/adminHome',
    name: 'AdminHome',
    component: AdminHomeView,
    children:[
      {
        path:'orderList',
        name: 'OrderList',
        component: OrderListView
      },
      {
        path: 'verificationCode',
        name: 'VerificationCode',
        component: VerificationCode
      },
      {
        path: 'account',
        name: 'Account',
        component: AccountView
      }
    ]
  },
  {
    path: '/adminLogin',
    name: 'AdminLogin',
    component: AdminLoginView
  },

  // 根路径：根据角色重定向
  {
    path: '/',
    redirect: () => {
      const store = require('@/store').default; // 引入 store
      const role = store.state.role;

      if (role == 'CUSTOMER') {
        return { name: 'UserUpload' };
      } else {
        return { name: 'OrderList' };
      }
    }
  }
]

const router = new VueRouter({
  mode: 'history', // 去掉 # 符号
  base: process.env.BASE_URL,
  routes
})

export default router